package com.yuchuang.config;

import com.yuchuang.interceptor.LoginHandlerInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
/**
 *@authoe    :cy
 *@date      :13:45 2023-04-25
 *description:@Configuration 用于定义配置类，可替换XML配置文件，被注解的类内部包含一个或多个@Bean注解方法。
 *return     :
 **/
public class MyMvcConfig implements WebMvcConfigurer {
    @Autowired
    private LoginHandlerInterceptor loginHandlerInterceptor;
    @Override
    public void addInterceptors(InterceptorRegistry registry){
//        addPathPatterns方法指定要拦截那些请求， /**代表所有请求都拦截
//         excludePathPatterns方法指定排除掉哪些请求不拦截。
        InterceptorRegistration interceptor = registry.addInterceptor(loginHandlerInterceptor);
        interceptor.addPathPatterns("/**");
        interceptor.excludePathPatterns("/");
        interceptor.excludePathPatterns("/index.html");
        interceptor.excludePathPatterns("/swagger-ui.html");
        interceptor.excludePathPatterns("/swagger/**");
        interceptor.excludePathPatterns("/webjars/**"); //允许通过
        interceptor.excludePathPatterns("/swagger-resources/**");
        interceptor.excludePathPatterns("/user/login");
        interceptor.excludePathPatterns("/upload/**");
        interceptor.excludePathPatterns("/images/**");
//        interceptor.excludePathPatterns("/asserts/**");
//        interceptor.excludePathPatterns("/webjars/**");

    }
//    实现 WebMvcConfigurer类后无法访问Swagger页面问题   解决  加一个addResourceHandlers
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("swagger-ui.html")
                .addResourceLocations("classpath:/META-INF/resources/swagger-ui.html");
        registry.addResourceHandler("/webjars/**")
                .addResourceLocations("classpath:/META-INF/resources/webjars/");
        System.out.print("1111111111111111111111111"+System.getProperty("user.dir"));
        // images/** 为前端访问路径   后面file:xxx为本地磁盘映射路径
        registry.addResourceHandler("/images/**")
                .addResourceLocations("file:" + System.getProperty("user.dir")+"\\src\\main\\resources\\static\\images\\");
    }
}
